package wqh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.AlarmEvent;
import wqh.domain.AlarmEventConfirm;
import wqh.global.GlobalSession;
import wqh.pojo.MapRet;
import wqh.service.AlarmEventConfirmService;
import wqh.service.AlarmEventService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/alarm_event_confirm")
public class AlarmEventConfirmController {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(AlarmEventConfirmService.class);

    @Autowired
    private AlarmEventService alarmEventService;

    @Autowired
    private AlarmEventConfirmService alarmEventConfirmService;

    @RequestMapping(value = "/getData",method = RequestMethod.GET)
    public Object getData(@RequestParam(value = "draw",required = true) int draw,
                          @RequestParam(value = "start",required = true) int start,
                          @RequestParam(value = "length",required = true) int length
//                          @RequestParam(value = "order[0][column]",required = true) int column,
//                          @RequestParam(value = "order[0][dir]",required = true) String order,
//                          @RequestParam(value = "search[value]",required = true) String search
    )
    {
        try {
            PageHelper.startPage(start / length + 1, length);
            List<AlarmEventConfirm> alarmEventConfirmList = alarmEventConfirmService.findAllRecord();
            Integer count = alarmEventConfirmService.selectCount();
            @SuppressWarnings("unchecked")
            PageInfo page = new PageInfo(alarmEventConfirmList);
            return MapRet.DATATABLES(draw,count,new Long(page.getTotal()).intValue(),alarmEventConfirmList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("显示报警事件异常：" + e.getMessage());
            return MapRet.SQL_ERROR(e);
        }
    }

    @RequestMapping(value = "/confirmOne",method = RequestMethod.GET)
    public Object confirmOne(HttpServletRequest request, @RequestParam(value = "id",required = true) int id)
    {
        try {
            AlarmEvent alarmEvent = alarmEventService.findById(id);
            if(alarmEvent != null){
                String sessionID = request.getSession().getId();
                String user = GlobalSession.getUserNameBySessionID(sessionID);
                if(user == null) user = "空用户";
                AlarmEventConfirm alarmEventConfirm = new AlarmEventConfirm(alarmEvent,user);
                alarmEventConfirmService.insertRecord(alarmEventConfirm);
                alarmEventService.deleteById(id);
            }
            return MapRet.SQL_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("确认报警事件错误:" + e.getMessage());
            return MapRet.SQL_ERROR();
        }
    }

    @RequestMapping(value = "/confirmChannel",method = RequestMethod.POST)
    public Object confirmChannel(HttpServletRequest request,
                                 @RequestParam(value = "ip",required = true) String ip,
                                 @RequestParam(value = "channel",required = true) byte channel_number)
    {
        try {
            List<AlarmEvent> alarmEventList = alarmEventService.findByIpAndChannel(ip,channel_number);
            if(alarmEventList.size() != 0){
                for(AlarmEvent alarmEvent : alarmEventList){
                    String sessionID = request.getSession().getId();
                    String user = GlobalSession.getUserNameBySessionID(sessionID);
                    if(user == null) user = "空用户";
                    AlarmEventConfirm alarmEventConfirm = new AlarmEventConfirm(alarmEvent,user);
                    alarmEventConfirmService.insertRecord(alarmEventConfirm);
                    alarmEventService.deleteById(alarmEvent.getId());
                }
            }
            return MapRet.SQL_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("确认通道所有报警事件错误:" + e.getMessage());
            return MapRet.SQL_ERROR();
        }
    }

}
